﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using MSWord = Microsoft.Office.Interop.Word;
using System.IO;
using System.Reflection;
using Maticsoft.DBUtility;
using System.Data.OleDb;
using System.Threading.Tasks;
using System.Threading;

namespace WFAwebgather
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        object path; //文件路径变量
        string strContent; //文本内容变量
        MSWord.Application wordApp; //Word应用程序变量
        MSWord.Document wordDoc; //Word文档变量
        private void Form1_Load(object sender, EventArgs e)
        {
         

        }

        /** 
     * 获取key在str中出现次数。 
     * @param str 
     * @param key 
     * @return 
     */
        public static int getKeyCount(String str, String key) {  
          
        //1,定义变量。记录每一次找到的key的位置。  
        int index = 0;  
        //2,定义变量，记录出现的次数。  
        int count = 0;  
          
        //3,定义循环。只要索引到的位置不是-1，继续查找。  
        while((index = str.IndexOf(key,index))!=-1){  
              
            //每循环一次，就要明确下一次查找的起始位置。  
            index = index + key.Length;  
              
            //每查找一次，count自增。  
            count++;  
        }  
        return count;  
    }

        public static ArrayList getKeyList(String str, String key)
        {
            ArrayList List = new ArrayList();
            //1,定义变量。记录每一次找到的key的位置。  
            int index = 0;
 
            //3,定义循环。只要索引到的位置不是-1，继续查找。  
            while ((index = str.IndexOf(key, index)) != -1)
            {

                //每循环一次，就要明确下一次查找的起始位置。  
                index = index + key.Length;
                List.Add(index);
            }
            return List;
        }
        /// <summary>  
        /// 截取字符串中指定标签内的内容  
        /// </summary>  
        /// <param name="Content">需要截取的字符串</param>  
        /// <param name="start">开始标签</param>  
        /// <param name="end">结束标签</param>  
        /// <returns></returns>  
        public string GetStr(string Content, string start, string end)
        {
            int posStart = Content.IndexOf(start);
            if (posStart<0||posStart > Content.Length)
            {
                return "缺失";
            }
            int posEnd = Content.IndexOf(end);
            return Content.Substring(posStart+ start.Length, (posEnd - posStart- start.Length)).Trim();
        }
        ///过滤所有的html
        ///
        public static string CheckStr(string html)
        {
            System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[/s/S]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[/s/S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" no[/s/S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[/s/S]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[/s/S]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex6 = new System.Text.RegularExpressions.Regex(@"/<img[^/>]+/>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex7 = new System.Text.RegularExpressions.Regex(@"</p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex8 = new System.Text.RegularExpressions.Regex(@"<p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex9 = new System.Text.RegularExpressions.Regex(@"<[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            html = regex1.Replace(html, ""); //过滤<script></script>标记
            html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性
            html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件
            html = regex4.Replace(html, ""); //过滤iframe
            html = regex5.Replace(html, ""); //过滤frameset
            html = regex6.Replace(html, ""); //过滤frameset
            html = regex7.Replace(html, ""); //过滤frameset
            html = regex8.Replace(html, ""); //过滤frameset
            html = regex9.Replace(html, "");
            //html = html.Replace(" ", "");
            html = html.Replace("</strong>", "");
            html = html.Replace("<strong>", "");
            html = html.Replace(@"Save Table (XLS/CSV)", "");
           // html = html.Replace(@"&#8212:", "—");
            html = html.Replace(@"&#8212;", "—");
            return html;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Task writeTask = new Task(getWeb);
            writeTask.Start();
        }

        public  void getWeb()
        {    
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            button1.Enabled = false;
            DbHelperOleDb.connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" + Application.StartupPath + "/Data/Data.mdb";

            string sqlStr = "select * from Industry where Code<>''";

            DataSet ds = DbHelperOleDb.Query(sqlStr);

            richTextBox1.Text += "采集开始："+"\n";

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                richTextBox1.Text += "采集" + dr["Code"] + ":";


                string htmlStr = Cgather.GetHtmlSource2("https://www.onetonline.org/link/details/" + dr["Code"], Encoding.Default);
                //int starIndex = 0;
                //int countStr = 0;
                //int postion = 0;
                //for (int i = 0; i < htmlStr.Length;i = htmlStr.IndexOf(@"id=" + "\"" + "wrapper_", i))
                //{                

                //    countStr++;
                //}

                htmlStr = htmlStr.Replace("&nbsp;", "");
                //ArrayList list = getKeyList(htmlStr, "id=" + "\"" + "wrapper_");

                string resaultStr = CheckStr(GetStr(htmlStr, @"<!-- begin content -->", @"<!-- end content -->"));
                string[] strL = resaultStr.Split(new string[] { "\n" }, StringSplitOptions.None);

                path = Application.StartupPath + @"\GetPath\" + dr["Code"] + dr["Occupation"].ToString().Replace("/", "").Replace(" ", "").Replace("@", "").Replace("#", "").Replace("*", "") + ".docx"; //路径

                wordApp = new MSWord.Application(); //初始化
                                                    //如果已存在，则删除
                if (File.Exists((string)path))
                {
                    File.Delete((string)path);
                }
                //由于使用的是COM库，因此有许多变量需要用Missing.Value代替
                Object Nothing = Missing.Value;
                wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                //strContent = "使用C#向Word文档中写入文本\n";
                //wordDoc.Paragraphs.Last.Range.Text = strContent;
                //strContent = "写入第二行文本";
                //wordDoc.Paragraphs.Last.Range.Text = strContent;


                foreach (string item in strL)
                {
                    if (item.Trim().Length != 0)
                    {
                        //richTextBox1.Text += item + "\n";
                        strContent = item.Trim() + "\n";
                        string insertStr = "INSERT INTO [Translate] ( Sentence ) Values (@Sentence)";
                        OleDbParameter[] oleDbParameter = { new OleDbParameter("@Sentence", OleDbType.VarWChar) };
                        oleDbParameter[0].Value = item.Trim();
                        DbHelperOleDb.ExecuteSql(insertStr, oleDbParameter);

                        wordDoc.Paragraphs.Last.Range.Text = strContent;

                    }

                }
                //WdSaveFormat为Word 2007文档的保存格式
                object format = MSWord.WdSaveFormat.wdFormatDocumentDefault;
                //将wordDoc文档对象的内容保存为DOCX文档
                wordDoc.SaveAs(ref path, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                //关闭wordDoc文档对象
                wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
                //关闭wordApp组件对象

                richTextBox1.Text += "成功" + "\n";
                //MessageBox.Show(resaultStr);
                //for (int i = 0; i < list.Count-1; i++)
                //{
                //    int j = i + 1;

                //    string subStr = htmlStr.Substring((int)list[i],(int)list[j]-(int)list[i]);

                //    //string reportStr=GetStr(subStr,"\""+"reportdesc"+"\""+">","<span class=\"sm");
                //    //if (reportStr=="缺失")
                //    //{
                //    //    reportStr = GetStr(subStr, "\"" + "report" + "\"" + ">", "<span class=\"sm");
                //    //}
                //    textBox1.Text= subStr+CheckStr(subStr);
                //}

                //textBox1.Text= getKeyCount(htmlStr, @"id=" + "\"" + "wrapper_").ToString();

            }
            button1.Enabled = true;
        }
    }
}
